//===============================================================================
// Microsoft patterns & practices
// SharePoint Guidance
//===============================================================================
// Copyright (c) Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using SPGSharePointDataAccessQuickStart.Common.Constants;
using SPGSharePointDataAccessQuickStart.Repository.BusinessEntities;
using SPGSharePointDataAccessQuickStart.Repository.SharePoint;

namespace SPGSharePointDataAccessQuickStart.Repository
{
    public class AnnouncementRepository : BaseEntityRepository<Announcement>, IAnnouncementRepository
    {
        #region Public Properties

        protected override string ListName
        {
            get
            {
                return Lists.Announcements;
            }
        }

        #endregion

        #region Constructor

        public AnnouncementRepository(IListItemRepository listItemRepository)
            : base(listItemRepository)
        {

        }

        #endregion

        #region Methods

        /// <summary>
        /// Persists a new Announcement.
        /// </summary>
        /// <param name="announcement">The Announcement to persist.</param>
        /// <param name="web">The SPWeb for the location of the Announcement.</param>
        /// <returns>The ID of the new Announcement.</returns>
        public int Add(Announcement announcement, SPWeb web)
        {
            return AddListItem(announcement, web);
        }

        /// <summary>
        /// Returns an Announcement based on its ID.
        /// </summary>
        /// <param name="id">The ID of the Announcement to return.</param>
        /// <param name="web">The SPWeb for the location of the Announcement.</param>
        /// <returns>Announcement entity.</returns>
        public Announcement Get(int id, SPWeb web)
        {
            StringBuilder queryBuilder = new StringBuilder("<Where>");
            queryBuilder.Append("<Eq><FieldRef Name='ID'/>");
            queryBuilder.Append(string.Format("<Value Type='Integer'>{0}</Value></Eq>", id));
            queryBuilder.Append("</Where>");

            return GetListItem(queryBuilder.ToString(), web);
        }

        /// <summary>
        /// Persists a fully populated Announcement with updates.
        /// </summary>
        /// <param name="announcement">The fully populated Announcement to update.</param>
        /// <param name="web">The SPWeb for the location of the Announcement.</param>
        public void Update(Announcement announcement, SPWeb web)
        {
            UpdateListItem(announcement, web);
        }

        /// <summary>
        /// Deletes a Announcement.
        /// </summary>
        /// <param name="id">The ID of the Announcement to delete.</param>
        /// <param name="web">The SPWeb for the location of the Announcement.</param>
        public void Delete(int id, SPWeb web)
        {
            DeleteListItem(id, web);
        }

        protected override Dictionary<Guid, object> GatherParameters(Announcement entity, SPWeb web)
        {
            Dictionary<Guid, object> fields = new Dictionary<Guid, object>();
            fields.Add(Fields.Title, entity.Title);
            fields.Add(Fields.Body, entity.Body);

            return fields;
        }

        protected override Announcement PopulateEntity(SPListItem item)
        {
            Announcement annoucement = new Announcement();
            annoucement.Id = (int)item[Fields.Id];
            annoucement.Title = (string)item[Fields.Title];
            annoucement.Body = (string)item[Fields.Body];

            return annoucement;
        }

        #endregion
    }
}
